//
//  MyselfViewController.m
//  Lucky28
//
//  Created by zr on 2018/7/31.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "MyselfViewController.h"
#import "MyselfHeaderView.h"
#import "MyselfTableViewCell.h"
#import "UserInfoViewController.h"
#import "ManageTieZiViewController.h"
#import "SendSuggestViewController.h"
#import "SettingViewController.h"

@interface MyselfViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray * imageNameArray;
@property (nonatomic, strong)NSArray * showStringArray;
@property (nonatomic, strong)MyselfHeaderView * header;
@property (nonatomic, copy)NSURL * url;
@property (nonatomic, copy)NSString * name;
@property (nonatomic, copy)NSString * gender;
@property (nonatomic, copy)NSString * birthday;

@end

@implementation MyselfViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.showStringArray = @[@"用户信息",@"帖子管理",@"意见反馈",@"清理缓存",@"设置"];
    self.imageNameArray = @[@"yonghu",@"tiezi",@"yijian",@"huancu",@"shezhi"];
    
    [self loadData];
    [self setupUI];
}


- (void)loadData{
    
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSString * token = [userDefaults objectForKey:@"token"];
    
    NSDictionary * parametersDic = @{@"token":token};
    
    [[NetworkTool sharedTools] POST:@"/index/Member/info" parameters:parametersDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
//        NSLog(@"res===%@",responseObject);
        
        NSString * codeString = [NSString stringWithFormat:@"%@",responseObject[@"status"]];
        
        if ([codeString isEqualToString:@"200"]) {
            
            NSDictionary * data = responseObject[@"data"];
            
            NSString * imageUrl = data[@"headimg"];
            NSURL * url = [NSURL URLWithString:imageUrl];

            self.header.url = url;
            self.header.nameString = data[@"username"];
            self.url = url;
            self.gender = data[@"sex"];
            self.name = data[@"username"];
            self.birthday = data[@"birthday"];
            
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseObject[@"msg"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
            
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        HIDDENHUD;
        
        NSLog(@"error===%@",error);
    }];
    
}


- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 667 * SCREENH6 - 130) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.rowHeight = 55 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    if (@available(iOS 11.0, *)) {
        tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    if ([self getIsIpad]) {
        
        
    }else{
        
        tableView.scrollEnabled = NO;
    }
    self.tableView = tableView;
    
    [self.tableView registerClass:[MyselfTableViewCell class] forCellReuseIdentifier:@"MyselfListCell"];
    [self.tableView registerClass:[MyselfHeaderView class] forHeaderFooterViewReuseIdentifier:@"MyselfHeaderView"];
    
    [self.view addSubview:self.tableView];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return 5;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyselfTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyselfListCell" forIndexPath:indexPath];
    
    cell.titleLabel.text = self.showStringArray[indexPath.row];
    cell.iconImageView.image = ImageNamed(self.imageNameArray[indexPath.row]);
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 250 * SCREENH6;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    
    self.header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MyselfHeaderView"];

    return self.header;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HIDDEN_TAB;
    
    if (indexPath.row == 0) {
        
        UserInfoViewController * userInfoVC = [UserInfoViewController new];
        
        userInfoVC.url = self.url;
        userInfoVC.name = self.name;
        userInfoVC.birthday = self.birthday;
        userInfoVC.gender = self.gender;
        
        [self.navigationController pushViewController:userInfoVC animated:YES];
    }else if (indexPath.row == 1){
        
        ManageTieZiViewController * manageVC = [ManageTieZiViewController new];
        
        [self.navigationController pushViewController:manageVC animated:YES];
    }else if (indexPath.row == 2){
        
        SendSuggestViewController * suggestVC = [SendSuggestViewController new];
        
        [self.navigationController pushViewController:suggestVC animated:YES];
    }else if (indexPath.row == 4){
        
        SettingViewController * settingVC = [SettingViewController new];
        
        [self.navigationController pushViewController:settingVC animated:YES];
    }else if (indexPath.row == 3){
        
        MBProgressHUD * hud = [[MBProgressHUD alloc] initWithView:self.view];
        
        [self.view addSubview:hud];
        
        [hud showAnimated:YES];
        
        [hud hideAnimated:YES afterDelay:2];
    }
    
    SHOW_TAB;
}



- (BOOL)getIsIpad
{
    
    NSString *deviceType = [UIDevice currentDevice].model;
    
    
    
    if([deviceType isEqualToString:@"iPhone"]) {
        
        //iPhone
        
        return NO;
        
    }
    
    else if([deviceType isEqualToString:@"iPod touch"]) {
        
        //iPod Touch
        
        return NO;
        
    }
    
    else if([deviceType isEqualToString:@"iPad"]) {
        
        //iPad
        
        return YES;
        
    }
    
    return NO;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
